home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: nntp.coast.net!torn!sq!msb
- From: msb@sq.com (Mark Brader)
- Subject: Re: Tradition or what?
- Message-ID: <1996Feb14.233911.5533@sq.com>
- Organization: SoftQuad Inc., Toronto, Canada
- References: <1996Feb13.115611.73989@cc.usu.edu>
- Date: Wed, 14 Feb 1996 23:39:11 GMT
-
- Erik van Renselaar (erik@cc.usu.edu) writes:
- > Can anyone tell me what the use is of returning
- > the same value for the function and one of the
- > output parameters, like it is done in strcpy?
-
- strcpy() doesn't do that, but does something even less useful -- it
- returns a copy of one of its input arguments. (This argument happens to
- point to an area in which output is written, hence Erik's misstatement).
-
- In the case of the string functions, returning a pointer to the buffer
- permits expressions like
-
- strcat (strcat (strcat (strcpy (buf, s1), s2), s3), s4);
-
- without needing to mention buf four times. That's about it. Nowadays
- I think most people would prefer to write that on four separate lines,
- mentioning buf four times, or (as I would) to use
-
- sprintf (buf, "%s%s%s%s", s1, s2, s3, s4);
-
- instead; and some of us wish that all these functions (including sprintf())
- had been designed to return a pointer to the terminating '\0' that they
- write in the string.
-
- An instance where the output really is returned two ways is time():
-
- time_t x, y;
- x = time (&y);
-
- Both x and y get the same value. In this case, the reason is that
- the call time() existed on UNIX in an era when int, which was 16 bits,
- was the widest integer type, so there wasn't a numeric type that could
- serve as time_t does now. In those days time() was used like *this*:
-
- int z[2];
- time (z);
-
- If prototypes had existed then, time()'s would have been void time(int *).
- Later when long was available, time() was made to return its value for
- the sake of convenience, but it was also desired for the traditional
- call to continue working so that code using it didn't need to be fixed.
- Note that functions like ctime() take a time_t * rather than a time_t;
- the same reasons apply.
-
- Or at least, this is my understanding. I wasn't there.
- --
- Mark Brader, msb@sq.com, SoftQuad Inc., Toronto
- Until 3,000 million years ago we can say not a lot happened
- although further study would not come amiss. Then signs of life
- appeared, including some large reptiles and, very recently, bipeds.
- It is too soon to say whether these bipeds will play an important
- part in the world's story. -- Colin Morris in "History Today"
-
- My text in this article is in the public domain.
-